package com.wuliu.servlet;

import com.wuliu.dao.BusinessClassDAO;
import com.wuliu.dao.OrdersDAO;
import com.wuliu.dao.impl.BusinessClassDAOImpl;
import com.wuliu.dao.impl.OrdersDAOImpl;
import com.wuliu.model.BusinessClass;
import com.wuliu.model.Orders;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/page8")
public class OrdersServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BusinessClassDAO businessClassDAO= new BusinessClassDAOImpl();
        List<BusinessClass> businessClasses =businessClassDAO.findAll();
        req.setAttribute("businessClasses",businessClasses);
        req.getRequestDispatcher("/WEB-INF/view/orders.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sender_name = req.getParameter("sender_name");
//        System.out.println(sender_name);
        String sender_address = req.getParameter("sender_address");
        String sender_phone = req.getParameter("sender_phone");
        double weight= Double.valueOf(req.getParameter("weight"));
        String receiver_name = req.getParameter("recipient_name");
        String receiver_address = req.getParameter("recipient_address");
        String receiver_phone = req.getParameter("recipient_phone");
        String other = req.getParameter("other");
        String shipping_method = req.getParameter("shipping_method");
        String payment=req.getParameter("payment");
        String claim_goods=req.getParameter("claim_goods");
        String packaging_service=req.getParameter("packaging_service");
        Orders orders = new Orders(null,sender_name,sender_address,sender_phone,weight,receiver_name,receiver_address,receiver_phone,other,shipping_method,payment,claim_goods,packaging_service);
        OrdersDAO ordersDAO = new OrdersDAOImpl();
//        System.out.println(orders);
        ordersDAO.add(orders);
        req.getRequestDispatcher("/WEB-INF/view/orders.jsp").forward(req, resp);
    }
}

